﻿/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Material 2.0
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.12
import QtGraphicalEffects 1.0
import QtWinExtras 1.15
import "qrc:/MaterialUI/"
import "qrc:/MaterialUI/Interface/"

ApplicationWindow {
    id: window
    width: 860
    height: 600
    visible: true
    minimumWidth: 400
    minimumHeight: 300
    color: "#00000000"
    flags:Qt.Window | Qt.FramelessWindowHint | Qt.WindowSystemMenuHint | Qt.WindowMinimizeButtonHint | Qt.WindowMaximizeButtonHint
    //    property real _flh_margin: ((window.visibility === Window.Maximized) || (window.visibility === Window.FullScreen)) ? 0.0 : (1.0 / Screen.devicePixelRatio)
    property bool isLogining: false
    property int cornerMargins: 8
    property int borderMarginsIn: 2
    property int borderMarginsOut: 5

    onActiveChanged: {
        if(active){
            SqlContactModel.clearMessageSum(SqlContactModel.currentIndex)
            taskbar.onStop()
        }
    }

    Component.onCompleted: {
        //kong_w@foxmail.com
        //852266938@qq.com
        //        ChatManage.sendRegiste("852266938@qq.com","123")
        //        ChatManage.test()
        console.log("Component.onCompleted")

    }
    TaskbarButton {
        property real proValue: 0
        property int clickTimes: 3
        property bool isOnce:false

        function isRunning(){
            return(timer.running)
        }

        function onStartClick(value){
            taskbar.isOnce = value
            taskbar.proValue = 0;
            taskbar.clickTimes=3;
            timer.running = true;
        }

        function onStop(){
            taskbar.proValue=0;
            timer.running = false;
        }

        id: taskbar
//        overlay.iconSource: "qrc:/shared/test.ico"
//        overlay.accessibleDescription: qsTr("加载中...")
        progress.visible: (progress.value != 0)
        progress.value: taskbar.proValue

        Timer{
            id: timer
            running: false
            repeat: true
            interval: 200
            onTriggered:{
                if((taskbar.clickTimes--)>0){
                    taskbar.proValue=taskbar.proValue>0?0:100
                }
                else{
                    taskbar.proValue=100
                    timer.stop()
                    if(window.active)
                        taskbar.proValue=0
                    else{
                        if(taskbar.isOnce)
                            taskbar.proValue=0
                        else
                            taskbar.proValue=100
                    }

                }
            }
        }
    }

    Connections{
        target:SqlContactModel
        function onSendNewMessage(){
            taskbar.onStartClick(window.active)
        }
    }

    Connections{
        target: ChatManage
        function onGetError(type,subIndex,message){
            materialUI.showSnackbarMessage("["+type+":"+subIndex+"]"+message)
        }
    }
    Rectangle{
        id:mainRec
        anchors.fill: parent
        anchors.margins: 18
        RectangularGlow {
            z: -1
            anchors.fill: mainRec
            glowRadius: 6
            spread: 0.22
            color: "#20000000"
        }
        Loader {
            id: delegateLoader
            anchors.fill: parent
            sourceComponent: LoginMain{
                anchors.fill: parent
            }
        }

        MaterialUI {
            id: materialUI
            anchors.fill: parent
            dialogCancelText: "取消"
            dialogOKText: "确定"
        }
    }

    MouseArea{
        id:lefttop
        anchors{
            left:mainRec.left
            leftMargin: -borderMarginsOut
            right: mainRec.left
            rightMargin: -cornerMargins
            top:mainRec.top
            topMargin: -borderMarginsOut
            bottom: mainRec.top
            bottomMargin: -cornerMargins
        }
        cursorShape:Qt.SizeFDiagCursor
        property point startPoint: Qt.point(0,0)
        property int windowHeight
        property int windowWidth
        property int windowY
        property int windowX
        onPressed: {
            startPoint = mapToGlobal(mouseX,mouseY)
            windowHeight = window.height
            windowWidth = window.width
            windowY = window.y
            windowX = window.x
        }
        onPositionChanged: {
            if(pressed){
                var now = mapToGlobal(mouseX,mouseY)
                var offsetX = now.x - startPoint.x;
                var offsetY = now.y - startPoint.y;

                if ((windowWidth - offsetX) >= window.minimumWidth)
                {
                    window.x = windowX + offsetX;
                    window.width = windowWidth - offsetX;
                }
                if ((windowHeight - offsetY) >= window.minimumHeight && offsetY != 0)
                {
                    window.height = windowHeight - offsetY;
                    window.y = windowY + offsetY;
                }
            }
            mouse.accepted = false
        }
    }

    MouseArea{
        id:righttop
        anchors{
            left: mainRec.right
            leftMargin: -cornerMargins
            right:mainRec.right
            rightMargin: -borderMarginsOut
            top:mainRec.top
            topMargin: -borderMarginsOut
            bottom: mainRec.top
            bottomMargin: -cornerMargins
        }
        cursorShape:Qt.SizeBDiagCursor
        property point startPoint: Qt.point(0,0)
        property int windowHeight
        property int windowWidth
        property int windowY
        onPressed: {
            startPoint = mapToGlobal(mouseX,mouseY)
            windowHeight = window.height
            windowWidth = window.width
            windowY = window.y
        }
        onPositionChanged: {
            if(pressed){
                var now = mapToGlobal(mouseX,mouseY)
                var offsetX = now.x - startPoint.x;
                var offsetY = now.y - startPoint.y;
                if ((windowWidth + offsetX) >= window.minimumWidth && offsetX != 0)
                {
                    window.width = windowWidth + offsetX;
                }
                if ((windowHeight - offsetY) >= window.minimumHeight && offsetY != 0)
                {
                    window.height = windowHeight - offsetY;
                    window.y = windowY + offsetY;
                }
            }
            mouse.accepted = false
        }
    }
    MouseArea{
        id:rightbottom
        anchors{
            left: mainRec.right
            leftMargin: -cornerMargins
            right:mainRec.right
            rightMargin: -borderMarginsOut
            top: mainRec.bottom
            topMargin: -cornerMargins
            bottom: mainRec.bottom
            bottomMargin: -borderMarginsOut
        }
        cursorShape:Qt.SizeFDiagCursor
        property point startPoint: Qt.point(0,0)
        property int windowHeight
        property int windowWidth
        onPressed: {
            startPoint = mapToGlobal(mouseX,mouseY)
            windowHeight = window.height
            windowWidth = window.width
        }
        onPositionChanged: {
            if(pressed){
                var now = mapToGlobal(mouseX,mouseY)
                var offsetX = now.x - startPoint.x;
                var offsetY = now.y - startPoint.y;
                if ((windowWidth + offsetX) >= window.minimumWidth && offsetX != 0)
                {
                    window.width = windowWidth + offsetX;
                }
                if ((windowHeight + offsetY) >= window.minimumHeight && offsetY != 0)
                {
                    window.height = windowHeight + offsetY;
                }
            }
            mouse.accepted = false
        }
    }
    MouseArea{
        id:leftbottom
        anchors{
            left:mainRec.left
            leftMargin: -borderMarginsOut
            bottom:mainRec.bottom
            bottomMargin: -borderMarginsOut
            right: mainRec.left
            rightMargin: -cornerMargins
            top: mainRec.bottom
            topMargin: -cornerMargins
        }
        cursorShape:Qt.SizeBDiagCursor
        property point startPoint: Qt.point(0,0)
        property int windowHeight
        property int windowWidth
        property int windowX
        onPressed: {
            startPoint = mapToGlobal(mouseX,mouseY)
            windowHeight = window.height
            windowWidth = window.width
            windowX = window.x
        }
        onPositionChanged: {
            if(pressed){
                var now = mapToGlobal(mouseX,mouseY)
                var offsetX = now.x - startPoint.x;
                var offsetY = now.y - startPoint.y;
                if ((windowWidth - offsetX) >= window.minimumWidth && offsetX != 0)
                {
                    window.width = windowWidth - offsetX;
                    window.x = windowX + offsetX;
                }
                if ((windowHeight + offsetY) >= window.minimumHeight && offsetY != 0)
                {
                    window.height = windowHeight + offsetY;
                }

            }

            mouse.accepted = false
        }

    }

    MouseArea{
        id:top
        anchors{
            left:mainRec.left
            leftMargin: cornerMargins
            top:mainRec.top
            topMargin: -borderMarginsOut
            right:mainRec.right
            rightMargin: cornerMargins
            bottom: mainRec.top
            bottomMargin: -borderMarginsIn
        }
        cursorShape:Qt.SizeVerCursor
        property point startPoint: Qt.point(0,0)
        property int windowHeight
        property int windowY
        onPressed: {
            startPoint = mapToGlobal(mouseX,mouseY)
            windowHeight = window.height
            windowY = window.y
        }
        onPositionChanged: {
            if(pressed){
                var now = mapToGlobal(mouseX,mouseY)
                var offsetY = now.y - startPoint.y;
                if ((windowHeight - offsetY) >= window.minimumHeight && offsetY != 0)
                {
                    window.y = windowY + offsetY;
                    window.height = windowHeight - offsetY;
                }
            }
            mouse.accepted = false
        }
    }

    MouseArea{
        id:left
        anchors{
            left:mainRec.left
            leftMargin: -borderMarginsOut
            right: mainRec.left
            rightMargin: -borderMarginsIn
            top:mainRec.top
            topMargin: cornerMargins
            bottom: mainRec.bottom
            bottomMargin: cornerMargins
        }
        cursorShape:Qt.SizeHorCursor
        property point startPoint: Qt.point(0,0)
        property int windowWidth
        property int windowX
        onPressed: {
            startPoint = mapToGlobal(mouseX,mouseY)
            windowWidth = window.width
            windowX = window.x
        }
        onPositionChanged: {
            if(pressed){
                var now = mapToGlobal(mouseX,mouseY)
                var offsetX = now.x - startPoint.x;
                if ((windowWidth - offsetX) >= window.minimumWidth)
                {
                    window.x = windowX + offsetX;
                    window.width = windowWidth - offsetX;

                }
            }
            mouse.accepted = false
        }

    }
    MouseArea{
        id:right
        anchors{
            left: mainRec.right
            leftMargin: -borderMarginsIn
            right:mainRec.right
            rightMargin: -borderMarginsOut
            top:mainRec.top
            topMargin: cornerMargins
            bottom: mainRec.bottom
            bottomMargin: cornerMargins
        }
        cursorShape:Qt.SizeHorCursor
        property point startPoint: Qt.point(0,0)
        property int windowWidth
        onPressed: {
            startPoint = mapToGlobal(mouseX,mouseY)
            windowWidth = window.width
        }
        onPositionChanged: {
            if(pressed){
                var now = mapToGlobal(mouseX,mouseY)
                var offsetX = now.x - startPoint.x;
                if ((windowWidth + offsetX) >= window.minimumWidth && offsetX != 0)
                {
                    window.width = windowWidth + offsetX;
                }
            }

            mouse.accepted = false
        }

    }

    MouseArea{
        id:bottom
        anchors{
            left:mainRec.left
            leftMargin: cornerMargins
            right:mainRec.right
            rightMargin: cornerMargins
            bottom: mainRec.bottom
            bottomMargin: -borderMarginsOut
            top:mainRec.bottom
            topMargin: -borderMarginsIn
        }
        cursorShape:Qt.SizeVerCursor
        property point startPoint: Qt.point(0,0)
        property int windowHeight
        onPressed: {
            startPoint = mapToGlobal(mouseX,mouseY)
            windowHeight = window.height
        }
        onPositionChanged: {
            if(pressed){
                var now = mapToGlobal(mouseX,mouseY)
                var offsetY = now.y - startPoint.y;
                if ((windowHeight + offsetY) >= window.minimumHeight && offsetY != 0)
                {
                    window.height = windowHeight + offsetY;
                }
            }
            mouse.accepted = false
        }
    }
}


/*##^##
Designer {
    D{i:0;formeditorZoom:0.9}
}
##^##*/
